# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.

import re
from django import newforms as forms
from django.contrib.auth.models import User
from apps.core.models.billing import Billing
from libs.extensions import custom_fields

class PaymethodForm(forms.Form):
    name = forms.CharField(label="Nome*")
    address = forms.CharField(label="Indirizzo*")
    city = forms.CharField(label="Citt&agrave;*")
    provincia = custom_fields.ProvField(label="Provincia")
    cap = forms.CharField(label="CAP*")
    #partita_iva = forms.CharField(label="P. IVA / Cod. Fiscale*")
    codice_fiscale = forms.CharField(label="P. IVA / Cod. Fiscale*")
    telefono = forms.CharField(label="Telefono")
    Legale_rappresentante = forms.CharField(required=False)

    def __init__(self, *args, **kwargs):
        super(PaymethodForm, self).__init__(*args, **kwargs)
        try:
            self.user = kwargs['initial']['user']
            self.identifier = kwargs['initial']['id']
        except: pass
        
    def clean_cap(self):
        cap = self.cleaned_data.get('cap')
        if bool(re.match("^\d{5}", cap)):
            return cap
        raise forms.ValidationError("Inserire un CAP valido")
    
    def clean_codice_fiscale(self):
        piva = self.cleaned_data['codice_fiscale']
        if ( bool(re.match( "(^\d{11}$|^[A-Z]{6}[\d]{2}[A-Z][\d]{2}[A-Z][\d]{3}[A-Z]$)", piva))):
            return piva
        raise forms.ValidationError(u"La partita IVA o il codice fiscale inseriti non sono validi")        

    def clean_telefono(self):
        tel = self.cleaned_data.get('telefono')
        if bool(re.match("^(\+|\d{5,})", tel)):
            return tel
        raise forms.ValidationError("Il numero inserito non e' corretto")

    def save(self, *args, **kwargs):
        u = User.objects.get(username__iexact=self.user)
        try:
            b = Billing.objects.get(id=self.identifier)
        except:
            b = Billing()
        b.user = u
        b.name = self.cleaned_data['name']
        b.address = self.cleaned_data['address']
        b.city = self.cleaned_data['city']
        b.provincia = self.cleaned_data['provincia']
        b.partita_iva = ''#self.cleaned_data['partita_iva']
        b.codice_fiscale = self.cleaned_data['codice_fiscale']
        b.Legale_rappresentante = self.cleaned_data['Legale_rappresentante']
        b.cap = self.cleaned_data['cap']
        b.telefono = self.cleaned_data['telefono']
        b.save()
